package exam;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

class MergeUtil {
    static List<Goods> mergeGoods(List<Goods> goodsList) {
        ArrayList<Goods> goods2 = new ArrayList<>();
        HashSet<Integer> integers = new HashSet<>();
        for (int i = 0; i < goodsList.size(); i++) {
            if(integers.contains(i)){
                continue;
            }
            Goods goods = goodsList.get(i);
            for (int i1 = i+1; i1 < goodsList.size(); i1++) {
                Goods goods1 = goodsList.get(i1);
                if(goods.attList.containsAll(goods1.attList)
                && goods1.attList.containsAll(goods.attList) && goods1.name.equals(goods.name)){
                    goods.count += goods1.count;
                    integers.add(i1);
                }

            }
            goods2.add(goods);
        }
        return goods2;
    }

    public static void main(String[] args) {
        ArrayList<Goods> goods = new ArrayList<>();
        Goods good1 = new Goods();
        good1.count = 1;
        good1.name = "苹果";
        good1.attList = new ArrayList<>();
        good1.attList.add("1");
        good1.attList.add("3");
        good1.attList.add("2");

        goods.add(good1);

        good1 = new Goods();
        good1.count = 1;
        good1.name = "苹果";
        good1.attList = new ArrayList<>();
        good1.attList.add("2");
        good1.attList.add("1");
        good1.attList.add("3");

        goods.add(good1);

        good1 = new Goods();
        good1.count = 1;
        good1.name = "蓝莓";
        good1.attList = new ArrayList<>();
        good1.attList.add("2");
        good1.attList.add("1");
        good1.attList.add("3");

        goods.add(good1);

        List<Goods> goods1 = mergeGoods(goods);
        System.out.println(goods1);
    }
}